package com.example.dao.impl;

import com.example.dao.CostRecordDao;
import com.example.entity.customerAdmin.CostRecord;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CostRecordDaoImpl implements CostRecordDao{


    @Override
    public int add (CostRecord costRecord) {
        int i=-1;
        try {
            Object[] objects=new Object[6];
            objects[0]=costRecord.getName ();
            objects[1]=costRecord.getMoney ();
            objects[2]=costRecord.getType ();
            objects[3]=costRecord.getHappenTime ();
            objects[4]=costRecord.getStates ();
            objects[5]=costRecord.getUser ();
            String sql="insert into costrecord(name,money,type,happenTime,states,user) value(?,?,?,?,?,?)";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int del (int id) {
        int i=-1;
        try {
            Object[] objects=new Object[1];
            objects[0]=id;
            i= BaseDao.modify ("delete from costrecord where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (CostRecord costRecord) {
        int i=-1;
        try {
            Object[] objects=new Object[7];
            objects[0]=costRecord.getName ();
            objects[1]=costRecord.getMoney ();
            objects[2]=costRecord.getType ();
            objects[3]=costRecord.getHappenTime ();
            objects[4]=costRecord.getStates ();
            objects[5]=costRecord.getUser ();
            objects[6]=costRecord.getId ();
            String sql="update costrecord set name=?,money=?,type=?,happenTime=?,states=?,user=? where id=?";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<CostRecord> selectAll (int page, int limit) {
        List<CostRecord> costRecords=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from costrecord limit ?,?", objects);
        try {
            while (select.next ()){
                CostRecord contactUser=new CostRecord ();
                contactUser.setId (select.getInt ("id"));
                contactUser.setName (select.getString ("name"));
                contactUser.setMoney (select.getInt ("money"));
                contactUser.setType (select.getString ("type"));
                contactUser.setHappenTime (select.getString ("happenTime"));
                contactUser.setStates (select.getString ("states"));
                contactUser.setUser (select.getString ("user"));
                costRecords.add (contactUser);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return costRecords;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from costrecord ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public CostRecord selectId (int id) {
        CostRecord contactUser=new CostRecord ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from costrecord where id=?", objects);
        try {
            while (select.next ()){
                contactUser.setId (select.getInt ("id"));
                contactUser.setName (select.getString ("name"));
                contactUser.setMoney (select.getInt ("money"));
                contactUser.setType (select.getString ("type"));
                contactUser.setHappenTime (select.getString ("happenTime"));
                contactUser.setStates (select.getString ("states"));
                contactUser.setUser (select.getString ("user"));
            return contactUser;
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return null;
    }
}
